/*
 * Copyright (c) 2007 jNetX.
 * http://www.jnetx.com
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * jNetX. You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license
 * agreement you entered into with jNetX.
 *
 * $Id$
 */
package com.jnetx.javax.sip.header;

import junit.framework.TestCase;

import java.text.ParseException;

import com.jnetx.javax.sip.address.AddressFactoryImpl;

/**
 * @author <a href="mailto:dparhonin@jnetx.ru">Dmitry Parhonin</a>
 * @version $Revision$
 */
public class ContactHeaderParserTest extends TestCase {
    public ContactHeaderParserTest(String name) {
        super(name);
    }

    public void test() {
        final byte[] data = "Contact: \"Bob Marley\" <sip:bobm@jamaica.com>;q=1.0, Bob sips:marley@reggy.com;q=0.8, BM <mailto:bm@reggy.com>;q=0.5".getBytes();
        final ContactHeaderParser parser = new ContactHeaderParser();
        ContactHeaderImpl[] headers;
        try {
            headers = parser.parse(data, 8, data.length);
        } catch (ParseException e) {
            fail(e.getMessage());
            return;
        }
        assertNotNull("The result is null!", headers);
        assertEquals("The length of the result is wrong!", 3, headers.length);
        assertEquals("The display name is wrong!", "Bob Marley", headers[0].getAddress().getDisplayName());
        try {
            assertEquals("The address is wrong!", AddressFactoryImpl.createURILazily("sip:bobm@jamaica.com"), headers[0].getAddress().getURI());
        } catch (ParseException e) {
            fail("Cannot create the URI!");
        }
        assertEquals("The q value is wrong!", 1.0f, headers[0].getQValue());
        assertEquals("The string representation is wrong!", "Contact: \"Bob Marley\" <sip:bobm@jamaica.com>;q=1.0", headers[0].toString());

        assertEquals("The display name is wrong!", "Bob", headers[1].getAddress().getDisplayName());
        try {
            assertEquals("The address is wrong!", AddressFactoryImpl.createURILazily("sips:marley@reggy.com"), headers[1].getAddress().getURI());
        } catch (ParseException e) {
            fail("Cannot create the URI!");
        }
        assertEquals("The q value is wrong!", 0.8f, headers[1].getQValue());
        assertEquals("The string representation is wrong!", "Contact: \"Bob\" <sips:marley@reggy.com>;q=0.8", headers[1].toString());

        assertEquals("The display name is wrong!", "BM", headers[2].getAddress().getDisplayName());
        try {
            assertEquals("The address is wrong!", AddressFactoryImpl.createURILazily("mailto:bm@reggy.com"), headers[2].getAddress().getURI());
        } catch (ParseException e) {
            fail("Cannot create the URI!");
        }
        assertEquals("The q value is wrong!", 0.5f, headers[2].getQValue());
        assertEquals("The string representation is wrong!", "Contact: \"BM\" <mailto:bm@reggy.com>;q=0.5", headers[2].toString());
    }
}
/*
 * $Log$
 */